package com.cz.mydao.wrapper;

import com.cz.mydao.constant.SqlKey;
import com.cz.mydao.enumeration.DeleteType;
import com.cz.mydao.util.MyUtil;

/**
 * @descriptions: 删除条件封装类
 * @author: Joker.
 * @date: 2024/12/26 12:02
 * @version: 1.0
 */

public class DeleteWrapper extends AbstractWrapper<DeleteWrapper> {

    /**
     * 生成删除sql
     *
     * @param tableName 表名
     * @return sql
     */
    @Override
    public String buildSql(String tableName) {
        StringBuilder sqlBuilder = new StringBuilder(SqlKey.DELETE).append(SqlKey.FROM).append(tableName);

        // 拼接 where 条件
        if (!conditions.isEmpty()) {
            // 调用拼接where方法
            MyUtil.buildWhereSql(conditions, sqlBuilder);
        }
        return sqlBuilder.toString();
    }

    public String buildSql(String tableName, DeleteType deleteType) {
        StringBuilder sql;
        // 直接删除
        if (DeleteType.DIRECT_DELETE.equals(deleteType)) {
            sql = new StringBuilder(SqlKey.DELETE).append(SqlKey.FROM).append(tableName);
        } else {
            // 逻辑删除
            sql = new StringBuilder(SqlKey.UPDATE).append(tableName);
            sql.append(SqlKey.SET)
                    .append(SqlKey.IS_DELETE)
                    .append(SqlKey.EQUALS)
                    .append(SqlKey.MINUS_ONE);
        }
        // 拼接 where 条件
        if (!conditions.isEmpty()) {
            // 调用拼接where方法
            MyUtil.buildWhereSql(conditions, sql);
        }
        return sql.toString();
    }
}

